﻿using StuClassInfo.StuCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace StuClassInfo
{
    /// <summary>
    /// Ajax 的摘要说明
    /// </summary>
    public class Ajax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            JavaScriptSerializer js = new JavaScriptSerializer();

            //接收用户id
            int userid = Convert.ToInt32(context.Request.QueryString["id"]);

            #region EF：删除
            //1.实例化数据库访问上下文
            var db = new StuUseEntities();
            //2.根据id查询实体类
            var model = db.UserInfo.FirstOrDefault(s => s.userid == userid);
            if (model == null)
            {
                OperateResult Oresult = new OperateResult(Resultstate.Error, "该记录已删除");
                string json = js.Serialize(Oresult);
                context.Response.Write(json);
            }
            //3.给实体类标记为删除
            db.Entry<UserInfo>(model).State = System.Data.Entity.EntityState.Deleted;
            //4.SaveChanges
            if (db.SaveChanges() > 0)
            {
                OperateResult Oresult = new OperateResult(Resultstate.Success, "删除成功");
                string json = js.Serialize(Oresult);
                context.Response.Write(json);
            }
            else
            {
                OperateResult Oresult = new OperateResult(Resultstate.Error, "删除失败");
                string json = js.Serialize(Oresult);
                context.Response.Write(json);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}